//
//  APIClient.m
//  EcmCustomer
//
//  Created by 张进 on 15/9/18.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "APIClient.h"
#import "MBProgressHUD.h"
@interface APIClient()<UIAlertViewDelegate>

@end

@implementation APIClient
static AFNetworkReachabilityStatus _networkStatus;
static APIClient *_sharedClient = nil;

+(AFNetworkReachabilityStatus)networkStatus{
    if (!_sharedClient) {
        _sharedClient = [APIClient sharedClient];
    }
    return _networkStatus;
}

+ (instancetype)sharedClient {
    
    static dispatch_once_t onceToken;
    
    NSString * APIBaseURLString = @"http://www.ezhongyi591.com:8080/EChineseMedicine/";
    ;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:APIBaseURLString]];
        [_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];
        
        [_sharedClient.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusReachableViaWWAN:
//                    [Dialog simpleToast:@"网络已连接"];
                    NSLog(@"网络已连接WWAN");
                    _networkStatus = AFNetworkReachabilityStatusReachableViaWWAN;
                    break;
                    
                case AFNetworkReachabilityStatusReachableViaWiFi:
//                    [Dialog simpleToast:@"网络已连接"];
                    NSLog(@"网络已连接WiFi");
                    _networkStatus = AFNetworkReachabilityStatusReachableViaWiFi;
                    break;
                case AFNetworkReachabilityStatusNotReachable:
//                    [Dialog simpleToast:@"当前网络不可用,请联系管理员"];
                {
                    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"没有网络！请连接wifi或开启数据流量" message:@"" delegate:self cancelButtonTitle:@"确定"otherButtonTitles: nil];
                    [alert show];
                    _networkStatus = AFNetworkReachabilityStatusNotReachable;
                }
                    break;
                default:
                    break;
            }
        }];
        [_sharedClient.reachabilityManager startMonitoring];
        
    });
    
    _networkStatus = AFNetworkReachabilityStatusUnknown;
    
    return _sharedClient;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
}
@end
